package ch.fusun.baron.basic.client.ui.player;

import org.eclipse.swt.graphics.Image;

import ch.fusun.baron.basic.client.ui.Activator;
import ch.fusun.baron.core.injection.ReInjector;
import ch.fusun.baron.player.Dynasty;
import ch.fusun.baron.player.Player;
import ch.fusun.baron.player.api.Country;
import ch.fusun.baron.player.api.CountryService;
import ch.fusun.baron.player.api.PlayerService;
import ch.fusun.baron.property.api.PropertyService;

public enum PersonColumns implements ColumnProvider<Player> {
    NAME("Name", 100) {

        @Override
        public String getText(Player element) {
            return element.getName();
        }
    },
    DYNASTY("Dynasty", 100) {

        @Override
        public String getText(Player element) {
            PlayerService playerService = ReInjector.getInstance()
                    .getInstanceForClass(PlayerService.class);
            Dynasty dynasty = playerService.getDynasty(element);
            if (dynasty != null) {
                return dynasty.getName();
            }
            return "";
        }

    },
    SEX("Sex", 100) {
        @Override
        public String getText(Player element) {
            return element.getGender().toString();
        }
    },
    AGE("Age", 100) {
        @Override
        public String getText(Player element) {
            return "" + element.getAge();
        }
    },
    FLAG("Flag", 100) {
        @Override
        public Image getImage(Player element) {
            Player p = element;
            PlayerService playerService = ReInjector.getInstance()
                    .getInstanceForClass(PlayerService.class);
            return Activator.getImageForCOA(playerService.getDynasty(p)
                    .getCoatOfArms());
        }

        @Override
        public String getText(Player element) {
            return null;
        }
    },
    MARRIAGE("Marriage", 100) {
        @Override
        public String getText(Player element) {
            // TODO Auto-generated method stub
            return null;
        }
    },
    OWNERSHIP("Ownership", 100) {
        @Override
        public String getText(Player element) {
            String result = ""; //$NON-NLS-1$
            Player player = element;
            PropertyService propertyService = ReInjector.getInstance()
                    .getInstanceForClass(PropertyService.class);
            CountryService countryService = ReInjector.getInstance()
                    .getInstanceForClass(CountryService.class);
            for (Country country : countryService.getCountries()) {
                if (player.equals(propertyService.getOwnership(country))) {
                    result += country.getName();
                }
            }
            return result;
        }
    },
    DEAD("RIP", 100) {
        @Override
        public String getText(Player element) {
            return element.isDead() ? "dead" : "";
        }
    };

    private String title;
    private int bounds;

    private PersonColumns(String title, int bounds) {
        this.title = title;
        this.bounds = bounds;
    }

    @Override
    public String getTitle() {
        return this.title;
    }

    @Override
    public int getBounds() {
        return this.bounds;
    }

    @Override
    public Image getImage(Player element) {
        return null;
    }

    @Override
    public abstract String getText(Player element);
}
